package com.xsb.mvvmlibrary.base

import androidx.lifecycle.ViewModel
import com.xsb.mvvmlibrary.network.ApiRetrofit
import com.xsb.mvvmlibrary.utils.SingleLiveEvent
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 *     author : 薛少博
 *     e-mail : xsb@zlhopesun.com
 *     time   : 2021/5/19
 *     desc   : 类说明
 *
 */
open class BaseViewModel : ViewModel() {
    val showDialog by lazy { SingleLiveEvent<String>() }
    val hideDialog by lazy { SingleLiveEvent<Boolean>() }

    /**
     * 创建ApiRetrofit
     */
    protected fun <T> create(clazz: Class<T>?): T {
        return ApiRetrofit.getInstance()?.retrofit!!.create(clazz)
    }

    protected fun dispose(observable: Observable<*>): Observable<out Any> {
            return observable.subscribeOn(Schedulers.io())
                .doOnSubscribe({ showDialog.postValue("加载中...") })
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally({ hideDialog.postValue(true) })
    }
}
